package cn.mw.cmdb.param;


import cn.mw.cmdb.entity.*;
import cn.mw.cmdb.enums.ViewTypeEnum;
import cn.mw.cmdb.service.ServiceChoose;
import cn.mw.cmdb.service.impl.ModelPropertyServiceImpl;
import lombok.Data;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.annotation.Transient;

import java.util.List;
import java.util.Set;

@Data
public class ModelPropertyParam implements ServiceChoose {
    //属性模型id
    @Transient
    private String modelId;
    //当前模型id
    @Transient
    private String ownModelId;
    //当前模型名称
    @Transient
    private String ownModelName;

    private String distinctId;

    //合并属性对应的模型名称
    private String mergedModelName;

    //属性id
    private String id;

    private List<String> ids;

    private List<ModelPropertyDeleteParam> delParams;

    //属性名称
    private String name;
    //属性是否显示  关闭后，查询新增修改列表全都不显示
    private Boolean enableShow;
    //属性list列表默认显示，如果enableShow为false,该字段不生效
    //实例列表表头字段显示
    private Boolean defaultListVisible;
    //告警标识
    private Boolean enableAlert;


    //字段是否纳入流程管理
    private Boolean enableProManage;
    //属性分类
    private String propertyCatologName;

    // 属性分类
    private List<PropertyCategoryVo> propertyCatologList;

    //属性类型
    private PropertyTypeRef propertyTypeRef;

    //字段权限
    private Set<FieldAuthType> fieldAuthTypes;

    //视图限制
    private Set<FieldViewAuthType> fieldViewAuthTypes;

    //视图限制
    private List<FieldViewAuthVo> fieldViewAuthList;

    //金融元父模型视图限制
    private List<FinanceModelFieldViewAuthVo> financeViewAuthList;

    //系统内部控制属性
    private Boolean sysInternal;

    //系统内部控制属性,是否关键字搜索
    private Boolean keywordSearch;

    //字段来源(自定义，内置，父模型)
    private String propertySource;

    private Integer sortNum;
    //当该属性值需要从外部服务获取时使用
    private ForeignKey foreignKey;

    @Override
    public String getServiceId() {
        return ModelPropertyServiceImpl.ID;
    }

    public void setPropertyCatologList(List<PropertyCategoryVo> propertyCatologList) {
        this.propertyCatologList = propertyCatologList;
        if (CollectionUtils.isNotEmpty(propertyCatologList)) {
            propertyCatologList.forEach(item -> {
                if (StringUtils.equals(item.getViewType(), ViewTypeEnum.instanceView.getName())) {
                    this.propertyCatologName = item.getPropertyCatologName();
                }
            });
        }
    }

    public void setFieldViewAuthList(List<FieldViewAuthVo> fieldViewAuthList) {
        this.fieldViewAuthList = fieldViewAuthList;
        if (CollectionUtils.isNotEmpty(fieldViewAuthList)) {
            fieldViewAuthList.forEach(item -> {
                if (StringUtils.equals(item.getViewType(), ViewTypeEnum.instanceView.getName())) {
                    this.fieldViewAuthTypes = item.getFieldViewAuthTypes();
                }
            });
        }
    }

    public void setFileViewAuth(String viewType, String financeParentModelId) {
        // 当配置了金融元父模型权限时以父模型配置为主
        if (StringUtils.isNotEmpty(financeParentModelId) && CollectionUtils.isNotEmpty(getFinanceViewAuthList())) {
            for (FinanceModelFieldViewAuthVo fieldViewAuthVo : getFinanceViewAuthList()) {
                if (financeParentModelId.equals(fieldViewAuthVo.getModelId())) {
                    setFieldViewAuthTypes(fieldViewAuthTypes);
                    return;
                }
            }
        }
        if (CollectionUtils.isNotEmpty(getFieldViewAuthList())) {
            for (FieldViewAuthVo fieldViewAuthVo : getFieldViewAuthList()) {
                if (viewType.equals(fieldViewAuthVo.getViewType())) {
                    fieldViewAuthTypes = fieldViewAuthVo.getFieldViewAuthTypes();
                    setFieldViewAuthTypes(fieldViewAuthTypes);
                    return;
                }
            }
        }
    }
}
